<?php


namespace app\admin\model\management;


use app\common\model\TimeModel;
use app\common\constants\MenuConstant;
use think\facade\Db;
use app\admin\model\management\SpecItem;
class Spec extends TimeModel
{
    // protected $pk = 'cate_id';
    protected $name = 'spec';
  
    public static function onAfterInsert($data)
    {   
        $spec=[];
    	$content=explode(',',$data['content']);
        foreach ($content as $k => $v) {
            $spec[]=[
                'spec_id'=>$data->id,
                'item'=>$v,
            ];
        }
        $SpecItem=new SpecItem();
        $SpecItem->saveAll($spec);
    }
    public static function onAfterUpdate($data)
    {    
      
        $spec=[];
    	$content=explode(',',$data['content']);
        foreach ($content as $k => $v) {
            $spec[]=[
                'spec_id'=>$data->id,
                'item'=>$v,
            ];
        }
        $SpecItem=new SpecItem();
        foreach ($spec as $k => $v) {
            $SpecItem->where(['spec_id'=>$v['spec_id'],'item'=>$v['item']])->delete();
        }
        $SpecItem->saveAll($spec);
    }
    public static function onAfterDelete($data){
        $SpecItem=new SpecItem();
        $SpecItem->where('spec_id',$data->id)->delete();
    }
      //所选分类下的规格
    // public function getSpec($cate_id){
    
    //     $where['cate_id']=$cate_id;
    //     $list  = $this->field('id,title')->order('sort asc,id asc')->where($where)->select()->toArray();
    //     $one=$this->field('id,title')->order('id asc')->where('cate_id',0)->select()->toArray();
    //     if($one){
    //         $list=array_merge($list,$one);
    //     }
    //     foreach ($list as $k => $v) {
    //         $item=Db::name('spec_item')->where('spec_id',$v['id'])->column('item');
    //         $list[$k]['content']=implode(',',$item);
    //     }
    //     return $list;
    // }
    
}